package com.kcwl.idea.plugins.gateway;

import cn.hutool.core.util.StrUtil;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationAction;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.kcwl.idea.plugins.gateway.config.KcGatewaySettings;
import org.jetbrains.annotations.NotNull;

public class UploadApiToGatewayAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        KcGatewaySettings settings = KcGatewaySettings.getInstance();
        if (StrUtil.isBlank(settings.getGatewayUrl())) {
            Notification notification = new Notification("Print", "Api上传", "请配置网关地址", NotificationType.INFORMATION);
            notification.addAction(new OpenGatewaySettingAction());
            Notifications.Bus.notify(notification, e.getProject());
            return;
        }
        GatewayEditForm gatewayEditForm = new GatewayEditForm(e);
        gatewayEditForm.showAndGet();
    }

    static class OpenGatewaySettingAction extends NotificationAction {
        OpenGatewaySettingAction() {
            super("打开网关地址配置界面");
        }
        @Override
        public void actionPerformed(@NotNull AnActionEvent e, @NotNull Notification notification) {
            ShowSettingsUtil.getInstance().showSettingsDialog(e.getProject(), "Gateway Settings");
            notification.expire();
        }
    }

}
